package com.lxb.vioce.view;

import android.app.ActivityOptions;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.lxb.vioce.model.SpeechResultBean;
import com.lxb.vioce.utils.GsonUtil;
import com.lxb.vioce.utils.StringUtil;

/**
 * Created by Administrator on 2017/3/15.
 */

public class SpeechService extends Service {
    SpeechRecognizer mIat;
    private Intent intent = new Intent("com.example.communication.RECEIVER");
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("SpeechService", "-----2----->>");
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d("SpeechService", "-----1----->>");

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("SpeechService", "-----3----->>");

        //1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener
        mIat = SpeechRecognizer.createRecognizer(this, null);
        //2.设置听写参数，详见《科大讯飞MSC API手册(Android)》SpeechConstant类
        mIat.setParameter(SpeechConstant.DOMAIN, "iat");
        mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");
        //3.开始听写
        mIat.startListening(mRecoListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("SpeechService", "-----4----->>");
    }

    //听写监听器
    private RecognizerListener mRecoListener = new RecognizerListener() {
        //听写结果回调接口(返回Json格式结果，用户可参见附录12.1)；
        //一般情况下会通过onResults接口多次返回结果，完整的识别内容是多次结果的累加；
        //关于解析Json的代码可参见MscDemo中JsonParser类；
        //isLast等于true时会话结束。
        public void onResult(RecognizerResult results, boolean isLast) {
            Log.d("Result:", results.getResultString());
            if (!StringUtil.isEmpty(results.getResultString())) {
                SpeechResultBean srb = GsonUtil.fromJson(results.getResultString(), SpeechResultBean.class);
                StringBuffer sb = new StringBuffer();
                if (srb.getWs() != null && srb.getWs().size() > 0) {
                    for (int i = 0; i < srb.getWs().size(); i++) {
                        sb.append(srb.getWs().get(i).getCw().get(0).getW());
                    }

                }
//                speechWordsTv.setText(sb.toString());
                intent.putExtra("speech", sb.toString());
                intent.putExtra("isLast", isLast);
                sendBroadcast(intent);
//                if (isLast) {
////                    Intent intent = new Intent(MainActivity.this, OtherActivity.class);
////                    startActivity(intent,
////                            ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, shareView, "shareName").toBundle());
//                }
            } else {
                intent.putExtra("speech", "抱歉我没听清,请再说一遍");
            }

        }

        //会话发生错误回调接口
        public void onError(SpeechError error) {
            error.getPlainDescription(true); //获取错误码描述
        }

        @Override
        public void onVolumeChanged(int i, byte[] bytes) {

        }

        //开始录音
        public void onBeginOfSpeech() {

        }

        //结束录音
        public void onEndOfSpeech() {

        }

        //扩展用接口
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {

        }
    };
}
